package com.youlai.boot.factory.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.factory.mapper.SettlementItemsMapper;
import com.youlai.boot.factory.service.SettlementItemsService;
import com.youlai.boot.factory.model.entity.SettlementItems;
import com.youlai.boot.factory.model.form.SettlementItemsForm;
import com.youlai.boot.factory.model.query.SettlementItemsQuery;
import com.youlai.boot.factory.model.vo.SettlementItemsVO;
import com.youlai.boot.factory.converter.SettlementItemsConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 结算单明细服务实现类
 *
 * @author youlaitech
 * @since 2025-07-06 15:40
 */
@Service
@RequiredArgsConstructor
public class SettlementItemsServiceImpl extends ServiceImpl<SettlementItemsMapper, SettlementItems> implements SettlementItemsService {

    private final SettlementItemsConverter settlementItemsConverter;

    /**
    * 获取结算单明细分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SettlementItemsVO>} 结算单明细分页列表
    */
    @Override
    public IPage<SettlementItemsVO> getSettlementItemsPage(SettlementItemsQuery queryParams) {
        Page<SettlementItemsVO> pageVO = this.baseMapper.getSettlementItemsPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取结算单明细表单数据
     *
     * @param id 结算单明细ID
     * @return 结算单明细表单数据
     */
    @Override
    public SettlementItemsForm getSettlementItemsFormData(Long id) {
        SettlementItems entity = this.getById(id);
        return settlementItemsConverter.toForm(entity);
    }
    
    /**
     * 新增结算单明细
     *
     * @param formData 结算单明细表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveSettlementItems(SettlementItemsForm formData) {
        SettlementItems entity = settlementItemsConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新结算单明细
     *
     * @param id   结算单明细ID
     * @param formData 结算单明细表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateSettlementItems(Long id,SettlementItemsForm formData) {
        SettlementItems entity = settlementItemsConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除结算单明细
     *
     * @param ids 结算单明细ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSettlementItemss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的结算单明细数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
